//
//  HREnditNickNameView.m
//  Hear
//
//  Created by mac on 2017/8/8.
//  Copyright © 2017年 mac. All rights reserved.
//

#import "HREnditNickNameView.h"
@interface HREnditNickNameView ()
@property (nonatomic, copy)SelectEnterBlock selectEnterBlock;
@property(nonatomic, strong)UITextField *textField;

@end
@implementation HREnditNickNameView
+(void)showEnditNickNameView:(SelectEnterBlock)selectEnterBlock{
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    HREnditNickNameView * showView = [[self alloc] initWithFrame:[UIScreen mainScreen].bounds];
    showView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.5];
    id view = [[window subviews] lastObject];
    if([view isKindOfClass:[HREnditNickNameView class]]){
        
        
        HREnditNickNameView *shareView = (HREnditNickNameView *)view;
        [shareView closeView];
        
    }else{
        [window addSubview:showView];
        
        [showView loadView:selectEnterBlock];
        
    }
}

- (void)loadView:(SelectEnterBlock)selectEnterBlock{
    _selectEnterBlock = selectEnterBlock;
    
    CGFloat leftPading = 50;
    CGFloat height = 145;
    
    UIView *backView = [[UIView alloc] initWithFrame:CGRectMake(leftPading, (kFullHeight-height)/2, (kFullWidth-leftPading*2), height)];
    backView.backgroundColor = [UIColor whiteColor];
    [self addSubview:backView];
    backView.layer.cornerRadius = 5;
    backView.clipsToBounds = YES;
    
    CGFloat topPading = 20;
    UITextField *textField = [[UITextField alloc] initWithFrame:CGRectMake(topPading, topPading, backView.width-topPading*2, 50)];
    textField.placeholder = @"给TA添加一个备注";
    textField.textColor = FirstTextColor;
    textField.font = CustomUIFont(14);
    [backView addSubview:(self.textField = textField)];
    textField.layer.cornerRadius = 5;
    textField.layer.borderColor = FirstTextColor.CGColor;
    textField.layer.borderWidth = 0.5;
    UIView *leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 20, textField.height)];
    textField.leftView = leftView;
    textField.leftViewMode = UITextFieldViewModeAlways;
    
    NSArray *descArr = @[@"取消",@"确定"];
    CGFloat buttonWidth = (backView.width-topPading*3)/2;
    for (int i = 0; i < 2; i ++) {
        UIButton *button = [UIButton buttonWithType:0];
        button.frame = CGRectMake(topPading+i*(buttonWidth+topPading), textField.bottom+topPading,buttonWidth , 35);
        button.tag = i+1;
        [button setTitle:descArr[i] forState:0];
        [button setTitleColor:[UIColor whiteColor] forState:0];
        button.titleLabel.font = CustomUIFont(14);
        [button addTarget:self action:@selector(buttonClick:) forControlEvents:UIControlEventTouchUpInside];
        button.layer.cornerRadius = 3;
        button.clipsToBounds = YES;
        [backView addSubview:button];
        if (i == 0){
            button.backgroundColor = [UIColor lightGrayColor];
        }else{
            button.backgroundColor = MainColor;
        }
    }
    
    
    
}

- (void)buttonClick:(UIButton *)btn{
    if (btn.tag == 1){
        [self closeView];
    }else{
        if (!self.textField.text.length){
            [MBProgressHUD showMessage:@"昵称不能为空" finishBlock:nil];
            return;
        }
        self.selectEnterBlock(self.textField.text);
        [self closeView];
    }
}


-(void)closeView
{
    [UIView animateWithDuration:0.3f animations:^{
        [self.subviews objectAtIndex:0].hidden = YES;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}
@end
